import random, pygame, sys
from pygame.locals import *

pygame.init()

FPS = 40 # frames per second setting
fpsClock = pygame.time.Clock()

FPS = 30 # frames per second to update the screen
WINWIDTH = 800 # width of the program's window, in pixels
WINHEIGHT = 600 # height in pixels
HALF_WINWIDTH = int(WINWIDTH / 2)
HALF_WINHEIGHT = int(WINHEIGHT / 2)


WHITE = (255, 255, 255)
BLACK = (0,  0, 0)
RED   = (255,0, 0)
GREEN = (0, 255, 0)

# set up the window
DISPLAYSURF = pygame.display.set_mode((WINWIDTH, WINHEIGHT), 0, 32)
pygame.display.set_caption('Animation')
BASICFONT = pygame.font.Font('freesansbold.ttf', 32)


gameOverSurf = BASICFONT.render('Game Over', True, WHITE)
gameOverRect = gameOverSurf.get_rect()
gameOverRect.center = (HALF_WINWIDTH, HALF_WINHEIGHT)

catImgLeft = pygame.image.load('cat.png')

catImgRight = pygame.transform.flip(catImgLeft, True, False)

ratImgLeft = pygame.image.load('blk_rat.png')
ratImgRight = pygame.transform.flip(ratImgLeft, True, False)

catImgTiny = pygame.image.load('cat_avator.png')

direction = 'right'

catx = 10
caty = 480

catWidth = 120
catHight = 80

ratx = HALF_WINWIDTH
raty = 0

ratWidth = 64
ratHight = 64

Score = 0

GameOver = False
GamePause = False

jump = False


lifecount = 3

ratSpeed = 5

# create the surfaces to hold game text
def drawSocre():
    global Score
    
    ScoreSurf = BASICFONT.render('Score: '+str(Score), True, GREEN)
    ScoreRect = ScoreSurf.get_rect()
    ScoreRect.center = (WINWIDTH - 150, 32)
    DISPLAYSURF.blit(ScoreSurf, ScoreRect)

def drawLife():
    global lifecount

    for i in range(0, lifecount):
        DISPLAYSURF.blit(catImgTiny,(20 + i * 40, 20))
        
jumpHeight = 10
jumpcount = jumpHeight
Scale = 0.5

def movement():
    global jump, jumpcount, caty, catx, direction, jumpHeight, ratSpeed, Scale
   
    keyspressed = pygame.key.get_pressed()
    
    if keyspressed[pygame.K_LEFT] and catx > 10:
        direction = 'left'
        catx -= ratSpeed
    if keyspressed[pygame.K_RIGHT] and catx < 680:
        direction = 'right'
        catx += ratSpeed    

    if not jump and keyspressed[pygame.K_UP]:
        jump = True
        if jumpHeight > 13:
            jumpHeight = 13
            jumpcount = 13
    
    if jump == True:            
        if jumpcount >=- jumpHeight:
            caty -= (jumpcount * abs(jumpcount))*Scale
            #print("====", caty)
            if caty > 480:
                caty = 480
            elif caty < 0:
                caty = 0
            jumpcount-=1
        else:
            jump = False
            jumpcount = jumpHeight
            

rat_dir = True

def rat_run():
    global raty, ratx, rat_dir, ratSpeed
    
    if rat_dir:
        ratx += ratSpeed
        if ratx > 760:
            rat_dir = False
    else:
        ratx -= ratSpeed
        if ratx < 0:
            rat_dir = True

def rat_drop():
    global raty
    if raty < 496:
        raty += 4

def gameOver():
    DISPLAYSURF.fill(BLACK)
    DISPLAYSURF.blit(gameOverSurf, gameOverRect)
    
DISPLAYSURF.fill(WHITE)
while True: # the main game loop
    
    DISPLAYSURF.fill(WHITE)
    drawSocre()
    drawLife()
    
    if direction == 'right':
        DISPLAYSURF.blit(catImgRight, (catx, caty))
    else:
        DISPLAYSURF.blit(catImgLeft,(catx, caty))

    if rat_dir:
        DISPLAYSURF.blit(ratImgRight, (ratx, raty))
    else:
        DISPLAYSURF.blit(ratImgLeft,(ratx, raty))
    
    #pygame.draw.circle(DISPLAYSURF, RED, (catx,caty), 2, 2)

    #DISPLAYSURF.blit(ratImg, (ratx, raty))
    #DISPLAYSURF.blit(ratImg, (ratx-20, raty-200))

    catRect = pygame.Rect(catx, caty, catWidth, catHight)
    ratRect = pygame.Rect(ratx, raty, ratWidth, ratHight)
    
    #pygame.draw.rect(DISPLAYSURF, RED, catRect)
    #pygame.draw.rect(DISPLAYSURF, GREEN, ratRect, 4)
    pygame.draw.line(DISPLAYSURF, BLACK, (10,10), (10, 200), 2)
    
    if catRect.colliderect(ratRect):
        if abs(catRect.centerx - ratRect.centerx) < 90 and catRect.centery < ratRect.centery - 50:
            Score += 10
 
            if Score % 50 == 0:
                ratSpeed += (Score/50) * 5
                
            if Score % 100 == 0:
                lifecount += 1
            #print("===  %d ==="%(Score))
            ratx = random.randint(50,600)
            raty = random.randint(50,200)
        else:
            if not lifecount:
                GameOver = True
            else:
                lifecount -= 1
                ratx = HALF_WINWIDTH;
                raty = 0
        
    
    for event in pygame.event.get():
        
        if event.type == KEYDOWN:
            '''
            if event.key == K_DOWN and caty < 500:
                caty = caty + 10
            if event.key == K_LEFT and catx > 10:
                catx = catx - 10
            if event.key == K_RIGHT:
                catx = catx + 10
            if event.key == K_UP:
                jump_up()
            '''
            if event.key == K_RETURN and GameOver:
                GameOver = False
                catx = 10
                caty = 480
                ratx = 300
                raty = 0
                Score = 0
                lifecount = 3
                ratSpeed = 5
                jumpHeight = 10
            if event.key == K_SPACE and not GameOver:
                GamePause = not GamePause
                
        elif event.type == QUIT:
            pygame.quit()
            sys.exit()
    
    if not GameOver:
        if not GamePause:
            rat_drop()
            rat_run()
            movement()
    else:
        gameOver()
    pygame.display.update()
    fpsClock.tick(FPS)
